package com.copa.gameStore.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author copa
 * @createDate 2022-10-20 10:29
 * @function 资源与角色匹配关系管理业务类
 */
@Service
public class ResourceServiceImpl {

    private Map<String, List<String>> resourceRolesMap;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @PostConstruct
    public void initData() {
        // 这里按理说应该动态查库然后插入redis的，但我为了和springboot数据库版本一致就先写死了
        resourceRolesMap = new TreeMap<>();
        // user
        resourceRolesMap.put("/api/user/update",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/user/account",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/user/recharge",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/user/resetPassword",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/user/getUserInfo",
                CollUtil.toList("USER", "ADMIN"));
        resourceRolesMap.put("/api/user/",
                CollUtil.toList("ADMIN"));
        // goods
        resourceRolesMap.put("/api/gameGoods/add",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/gameGoods/admin",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/gameGoods/admin/",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/gameGoods/update",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/cart/add",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/cart/get",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/cart/delete",
                CollUtil.toList("USER"));
        // order
        resourceRolesMap.put("/api/order/create",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/order/pay",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/order/list",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/order/detail",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/order/receive",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/order/refund",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/order/checkRefundList",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/order/checkRefund",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/order/admin/list",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/order/admin/detail",
                CollUtil.toList("ADMIN"));
        resourceRolesMap.put("/api/order/log",
                CollUtil.toList("USER"));
        resourceRolesMap.put("/api/order/admin/log",
                CollUtil.toList("ADMIN"));
        redisTemplate.opsForHash().putAll("AUTH:RESOURCE_ROLES_MAP", resourceRolesMap);
    }
}
